
package com.craterhill.java.ems.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.craterhill.java.ems.common.beans.Session;
import com.craterhill.java.ems.dao.SessionDao;

@Repository
public class SessionDaoImpl extends AbstractDao<Session> implements SessionDao {

	@Autowired
	public SessionDaoImpl(final DataSource dataSource) {
		super(dataSource);
	}

	public List<Session> findBySchool(final int schoolId) {
		return super.findMultipleObject(FIND_SESSION_BY_SCHOOL, new SessionExtractor(), schoolId);
	}

	@Override
	public Session find(int sessionId) {
		return super.findSingleObject(FIND_SESSION_BY_ID, new SessionExtractor(), sessionId);
	}

	@Override
	public int save(final Session session) {
		return super.save(new SessionPreparedStatementCreator(ACTION_SAVE, session));
	}

	@Override
	public boolean update(final Session session) {
		return super.update(new SessionPreparedStatementCreator(ACTION_UPDATE, session));
	}

	@Override
	public boolean remove(final Integer... sessionIds) {
		return super.batchOperation(DELETE_SESSION, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement preparedStatement, int position) throws SQLException {
				preparedStatement.setInt(1, sessionIds[position]);
			}

			@Override
			public int getBatchSize() {
				return sessionIds.length;
			}
		});
	}

	@Override
	public List<Session> findAll() {
		return super.findMultipleObject(FIND_ALL_SESSION, new SessionExtractor());
	}
}
